//
//  LGRegistViewController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/5/6.
//  账号注册

#import "LGRegistViewController.h"
#import "LGRegistInputView.h"

#define lineHeight  viewPix(50)
 
@interface LGRegistViewController ()
/**   */
@property (nonatomic , strong)UIView *topView;
/**   */
@property (nonatomic , strong)LGRegistInputView *phoneView;
/**   */
@property (nonatomic , strong)LGRegistInputView *codeView;
/**   */
@property (nonatomic , strong)LGRegistInputView *passwordView;
/**   */
@property (nonatomic , strong)LGRegistInputView *realnameView;
/**   */
@property (nonatomic , strong)LGRegistInputView *hospitalView;
/**   */
@property (nonatomic , strong)LGRegistInputView *postitleView;
/**   */
@property (nonatomic , strong)UIButton *registBtn;

/**   */
@property (nonatomic , assign)NSInteger seconds;
/**   */
@property (nonatomic , strong)NSTimer *timer;
@end

@implementation LGRegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"注册用户";
    self.view.backgroundColor = [UIColor colorWithString:@"#F2F3F7"];
    [self createSubView];
}

//获取验证码
-(void)getSmsCodeAction:(UIButton *)sender{
    [RequestUtil withPOST:@"/doctor/my.setting/verifyMobileSms" parameters:@{@"mobile":@""} withSign:NO success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.seconds = 60;
            [self.timer invalidate];
            self.timer = nil;
            self.codeView.actionBtn.selected = YES;
            self.codeView.actionBtn.userInteractionEnabled = NO;
            [self.codeView.actionBtn setTitle:[NSString stringWithFormat:@"重新发送(%ld)",self.seconds] forState:UIControlStateNormal];
            self.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(countDownAction) userInfo:nil repeats:YES];
            [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        
    }];
}

//验证码倒计时
-(void)countDownAction{
    self.seconds--;
    [self.codeView.actionBtn setTitle:[NSString stringWithFormat:@"重新发送(%ld)",self.seconds] forState:UIControlStateNormal];
    if (self.seconds == 0) {
        self.codeView.actionBtn.selected = NO;
        self.codeView.actionBtn.userInteractionEnabled = YES;
        [self.codeView.actionBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [self.timer invalidate];
        self.timer = nil;
    }
}

//判断手机格式
- (BOOL)checkTelNumber:(NSString *)telNumber{
    NSString *pattern = @"^1+[0123456789]+\\d{9}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",pattern];
    BOOL isMatch = [pred evaluateWithObject:telNumber];
    return isMatch;
}

-(void)registBtnAction{
    if(self.phoneView.contenTF.text.length == 0){
        [TooltipView showMessage:@"请输入手机号" offset:0];
        return;
        
    }else if (self.codeView.contenTF.text.length == 0){
        [TooltipView showMessage:@"请输入验证码" offset:0];
        return;
        
    }else if (self.passwordView.contenTF.text.length == 0){
        [TooltipView showMessage:@"请输入密码" offset:0];
        return;
        
    }else if (self.realnameView.contenTF.text.length == 0){
        [TooltipView showMessage:@"请输入真实姓名" offset:0];
        return;
        
    }else if (self.hospitalView.contenTF.text.length == 0){
        [TooltipView showMessage:@"请输入所在医院" offset:0];
        return;
        
    }else if (self.postitleView.contenTF.text.length == 0){
        [TooltipView showMessage:@"请输入职称" offset:0];
        return;
    }else{
        [TooltipView showMessage:@"保存成功，请等待审核" offset:0];
        [self.navigationController popViewControllerAnimated:YES];
    }
}


#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    [self.view addSubview:self.topView];
    [self.topView addSubview:self.phoneView];
    [self.topView addSubview:self.codeView];
    [self.topView addSubview:self.passwordView];
    [self.topView addSubview:self.realnameView];
    [self.topView addSubview:self.hospitalView];
    [self.topView addSubview:self.postitleView];
    [self.view addSubview:self.registBtn];
}

-(UIView *)topView{
    if(!_topView){
        _topView = [[UIView alloc] initWithFrame:CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), lineHeight*6)];
        [_topView setShadoWithColor:@"#B3C5D9" offset:CGSizeMake(0, 2) opacity:0.15 radius:15];
        _topView.backgroundColor = [UIColor whiteColor];
    }
    return _topView;
}

-(LGRegistInputView *)phoneView{
    if(!_phoneView){
        _phoneView = [[LGRegistInputView alloc] initWithFrame:CGRectMake(0, 0, Screen_W-viewPix(32), lineHeight)];
        _phoneView.type = @"phone";
        _phoneView.title = @"手机号";
    }
    return _phoneView;
}

-(LGRegistInputView *)codeView{
    if(!_codeView){
        _codeView = [[LGRegistInputView alloc] initWithFrame:CGRectMake(0, kMaxY(self.phoneView.frame), Screen_W-viewPix(32), lineHeight)];
        _codeView.type = @"code";
        _codeView.title = @"验证码";
    }
    return _codeView;
}

-(LGRegistInputView *)passwordView{
    if(!_passwordView){
        _passwordView = [[LGRegistInputView alloc] initWithFrame:CGRectMake(0, kMaxY(self.codeView.frame), Screen_W-viewPix(32), lineHeight)];
        _passwordView.backgroundColor = [UIColor whiteColor];
        _passwordView.type = @"password";
        _passwordView.title = @"密码";
    }
    return _passwordView;
}

-(LGRegistInputView *)realnameView{
    if(!_realnameView){
        _realnameView = [[LGRegistInputView alloc] initWithFrame:CGRectMake(0, kMaxY(self.passwordView.frame), Screen_W-viewPix(32), lineHeight)];
        _realnameView.type = @"text";
        _realnameView.title = @"真实姓名";
    }
    return _realnameView;
}

-(LGRegistInputView *)hospitalView{
    if(!_hospitalView){
        _hospitalView = [[LGRegistInputView alloc] initWithFrame:CGRectMake(0, kMaxY(self.realnameView.frame), Screen_W-viewPix(32), lineHeight)];
        _hospitalView.type = @"text";
        _hospitalView.title = @"所在医院";
    }
    return _hospitalView;
}

-(LGRegistInputView *)postitleView{
    if(!_postitleView){
        _postitleView = [[LGRegistInputView alloc] initWithFrame:CGRectMake(0, kMaxY(self.hospitalView.frame), Screen_W-viewPix(32), lineHeight)];
        _postitleView.type = @"text";
        _postitleView.title = @"职称";
    }
    return _postitleView;
}

-(UIButton *)registBtn{
    if(!_registBtn){
        _registBtn = [UIButton buttonWithTitle:@"注册" topMargin:kMaxY(self.topView.frame)+viewPix(70)];
        [_registBtn addTarget:self action:@selector(registBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registBtn;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
